#!/bin/bash
mydir=`dirname "$0"`
uninstallToolPath="$mydir/UninstallerTool"
installBasePath="${2%/Red Giant/PluralEyes 4/PremiereConnector}"
installSubPath="Red Giant/PluralEyes 4/PremiereConnector"
payloadFile="${PACKAGE_PATH}/../../Resources/Payloads/PluralEyes4PremiereConnector.connector.f92beb9d.pax.gz"
productName="PluralEyes4"
productNumber="116"
installProductStr=`/usr/bin/grep -i "\<${productName}\>" "/tmp/rgs.C867B87B_3A5F_42DF_A83E_089FEDA01D63_${productNumber}"`
if [ ! -z "${installProductStr}" ]; then
    /usr/bin/xattr -d com.apple.quarantine "${payloadFile}"
    /bin/mkdir -p "$2/${installSubPath}"
    pushd "$2/${installSubPath}"
    /bin/pax -rzf "${payloadFile}"
    popd
    "$uninstallToolPath" -a "shootersuite" "Shooter Suite" -a "shootersuite.pluraleyes4" "PluralEyes4" -a "shootersuite.pluraleyes4.StandaloneApplications" "PluralEyes4 for Standalone Applications" "${installBasePath}/${installSubPath}/AdobeExtensionManager" "${installBasePath}/${installSubPath}/com.redgiant.pe4" "${installBasePath}/${installSubPath}/PE4Panel.zxp" "${installBasePath}/Red Giant/PluralEyes 4/PremiereConnector" "${installBasePath}/Red Giant/PluralEyes 4" "${installBasePath}/Red Giant"


ExMgr="/Library/Application Support/Red Giant/PluralEyes 4/PremiereConnector/AdobeExtensionManager/Contents/MacOS/ExManCmd"
    CONNECTOR_FOLDER="/Library/Application Support/Red Giant/PluralEyes 4/PremiereConnector"
    ADOBE_EXTENSIONS_FOLDER="/Library/Application Support/Adobe/CEP/extensions"
    PE4_EXTENSION_FOLDER="com.redgiant.pe4"
    PREMIERE_CHECK=`ls /Applications | grep "Adobe Premiere Pro CC 2"`
    if [[ ! -z "${PREMIERE_CHECK}" ]]; then
    echo "Removing prior version of Premiere Pro extension for PluralEyes 4 from Adobe extensions folder..."
    rm -fR "${ADOBE_EXTENSIONS_FOLDER}/${PE4_EXTENSION_FOLDER}" || true
    echo "Removing any old jumpstart folder from connector folder..."
    rm -fR "${CONNECTOR_FOLDER}/${PE4_EXTENSION_FOLDER}/jumpstart" || true
    echo "Copying current Premiere Pro extension for PluralEyes 4 into Adobe extensions folder..."
    cp -R "${CONNECTOR_FOLDER}/${PE4_EXTENSION_FOLDER}" "${ADOBE_EXTENSIONS_FOLDER}" || true
    echo "Installing Premiere Pro extension for PluralEyes via Adobe ExManCmd..."
    chmod +x "${ExMgr}"
    "${ExMgr}" --install "${CONNECTOR_FOLDER}/PE4Panel.zxp" || true
    echo "Listing installed extensions..."
    "${ExMgr}" --list all || true
    echo "Completed install of Premiere Pro extension for PluralEyes"
    else
    echo "Adobe Premiere Pro CC or later not found, skipping PluralEyes Premiere connector installation."
    fi

fi
